# This file is intended to be a final submission. python tester.py Player.py
# should work at all times. If it does not, there is a bug.
# If you're just trying to test a solution, scroll down to the Player
# class.

# This file is intended to be in the same format as a valid solution, so
# that users can edit their solution into Player and then submit just this
# file to the contest. If you see any reason this would not work, please submit
# an Issue to https://github.com/ChadAMiller/hungergames/issues or email me.

# You can see more sample player classes in bots.py
import random
from decimal import Decimal, ROUND_DOWN
from itertools import izip

class BasePlayer(object):
    '''
    Base class so I don't have to repeat bookkeeping stuff.
    Do not edit unless you're working on the simulation.
    '''
    
    def __str__(self):
        try:
            return self.name
        except AttributeError:
            # Fall back on Python default
            return super(BasePlayer, self).__repr__()
    
    def hunt_choices(*args, **kwargs):
        raise NotImplementedError("You must define a strategy!")
        
    def hunt_outcomes(*args, **kwargs):
        pass
        
    def round_end(*args, **kwargs):
        pass


class Player(BasePlayer):
    '''
    Your strategy starts here.
    '''
    
    def __init__(self, name):
                    self.name = name
                    self.last_reps = ()
                    self.my_decisions = ()
                    self.upper_limit = 1
                    self.lower_limit = 0.6
    
    def hunt_choices(
                    self,
                    round_number,
                    current_food,
                    current_reputation,
                    m,
                    player_reputations,
                    ):
        '''Required function defined in the rules'''
        
        self.last_reps = tuple(player_reputations)
        # The upper limit is the maximum reputation or maybe a little lower
        self.upper_limit = Decimal(max(player_reputations)).quantize(Decimal('.01'),ROUND_DOWN)
        

        if round_number == 1:
            # In round 1 make a fair contribution to get the extra food
            prob = (m/(1.0+len(player_reputations)))/float(len(player_reputations))
            hunt_decisions = ['h' if random.random() < prob else 's' for rep in player_reputations]
            
        else:
            # If other player reputation is between limits then hunt with him            
            hunt_decisions = ['h' if self.lower_limit <= rep < self.upper_limit else 's' for rep in player_reputations]

        self.my_decisions = tuple(hunt_decisions)

        return hunt_decisions
        

    def hunt_outcomes(self, food_earnings):
        #calculating average earning of the round
        avg_earning = sum(food_earnings)/float(len(food_earnings))

        
        los=0;
        rep_acc=0;
        
        
        for lr,fe in izip(self.last_reps,food_earnings):
            if fe == -3 :
                rep_acc+=lr
                los+=1
                
        #average reputation
        rep_avg = (sum(self.last_reps) / float(len(self.last_reps))) 
        self.lower_limit = (2*self.lower_limit+rep_avg)/3
        
        #If I hunted and the other player didn't then adjust my lower limit 
        if los != 0:        
            avg_ll=rep_acc/los
            self.lower_limit = (self.lower_limit+avg_ll*3)/4
           
                
        
        
        

    def round_end(self, award, m, number_hunters):
        '''Required function defined in the rules'''
        pass
        
